﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace Ape.Volo.Common.Extensions;

public static partial class ExtObject
{
    /// <summary>
    /// 复制序列中的数据
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    /// <param name="iEnumberable">原数据</param>
    /// <param name="startIndex">原数据开始复制的起始位置</param>
    /// <param name="length">需要复制的数据长度</param>
    /// <returns></returns>
    public static IEnumerable<T> Copy<T>(this IEnumerable<T> iEnumberable, int startIndex, int length)
    {
        var sourceArray = iEnumberable.ToArray();
        T[] newArray = new T[length];
        Array.Copy(sourceArray, startIndex, newArray, 0, length);

        return newArray;
    }

    /// <summary>
    /// 给IEnumerable拓展ForEach方法
    /// </summary>
    /// <typeparam name="T">模型类</typeparam>
    /// <param name="iEnumberable">数据源</param>
    /// <param name="func">方法</param>
    public static void ForEach<T>(this IEnumerable<T> iEnumberable, Action<T> func)
    {
        foreach (var item in iEnumberable)
        {
            func(item);
        }
    }

    /// <summary>
    /// 给IEnumerable拓展ForEach方法
    /// </summary>
    /// <typeparam name="T">模型类</typeparam>
    /// <param name="iEnumberable">数据源</param>
    /// <param name="func">方法</param>
    public static void ForEach<T>(this IEnumerable<T> iEnumberable, Action<T, int> func)
    {
        var array = iEnumberable.ToArray();
        for (int i = 0; i < array.Count(); i++)
        {
            func(array[i], i);
        }
    }

    /// <summary>
    /// IEnumerable转换为List'T'
    /// </summary>
    /// <typeparam name="T">参数</typeparam>
    /// <param name="source">数据源</param>
    /// <returns></returns>
    public static List<T> CastToList<T>(this IEnumerable source)
    {
        return new List<T>(source.Cast<T>());
    }

    /// <summary>
    /// 将IEnumerable'T'转为对应的DataTable
    /// </summary>
    /// <typeparam name="T">数据模型</typeparam>
    /// <param name="iEnumberable">数据源</param>
    /// <returns>DataTable</returns>
    public static DataTable ToDataTable<T>(this IEnumerable<T> iEnumberable)
    {
        return iEnumberable.ToJson().ToDataTable();
    }
}
